import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {path:'/tabbar',redirect:'/tabbar/article'},
  {path:'/',redirect:'/login'},
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path:'/login',
    name:'Login',
    component:()=> import('../views/Login.vue')
  },
  {
    path:'/tabbar',
    name:'Tabbar',
    component:()=> import('../views/Tabbar.vue'),
    children:[
      {
        path: 'article',
        name: 'Article',
        component: () => import('../views/Articles/Article.vue')
      },
      {
        path: 'articleDetail',
        name: 'ArticleDetail',
        component: () => import('../views/Articles/ArticleDetail.vue')
      },
      {
        path: 'movie',
        name: 'Movie',
        component: () => import('../views/Movies/Movie.vue')
      },
      {
        path:'movieDetail',
        name:'MovieDetail',
        component: () => import('../views/Movies/MovieDetail.vue')
      },
      {
        path:'more',
        name:'More',
        component: () => import('../views/Movies/More.vue')
      },
      {
        path:'search',
        name:'Search',
        component: () => import('../views/Movies/Search.vue')
      },
      {
        path: 'music',
        name: 'Music',
        component: () => import('../views/Musics/Music.vue')
      },
      {
        path: 'musicDetail',
        name: 'MusicDetail',
        component: () => import('../views/Musics/MusicDetail.vue')
      },
      {
        path: 'personal',
        name: 'Personal',
        component: () => import('../views/Personal/My.vue')
      }
    ]
  }
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
var vm = new Vue()
router.beforeEach((to,from,next)=>{
  var auth = vm.$cookie.get('loginAuth')   // 使用vue-cookie获取cookie
  console.log(auth);
  console.log(to.path);
  if(to.path == '/login'){
    if(auth){
      router.push('/home')
    }else{
      next()
    }
  }else{
    if(auth){
      next()
    }else{
      router.push('/login')
    }
  }
})
export default router
